package com.vecspace.utils;

import java.util.UUID;

public class UUIDUtil {
     /** 
     * 获得一个32位长度的UUID 
     * @return String UUID 
     */ 
    public static String createUUID(){
        String s = UUID.randomUUID().toString(); 
        //去掉“-”符号 
        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24); 
    } 
    
    /** 
     * 获得一个原始36位长度的UUID 
     * @return String UUID 
     */ 
    public static String createUUID36(){
        String s = UUID.randomUUID().toString(); 
        return s;
    } 
    private static String digits(long val, int digits) {
        long hi = 1L << (digits * 4);
        return Numbers.toString(hi | (val & (hi - 1)), Numbers.MAX_RADIX).substring(1);
    }
    
    /**
     * 以62进制（字母加数字）生成19位UUID，最短的UUID 例子： 9kH63UK4BXx1S62Lzyk dLo0JUu0OqLemJGxixo
     * n7Fw32kRj1bMSKFfTLb
     * 此函数生成的uuid不能用于文件名（因为文件名不区分大小写，但是此函数生存的uuid是区分大小写的）
     * @return
     */
    public static String createCompressedUUID() {
        UUID uuid = UUID.randomUUID();
        StringBuilder sb = new StringBuilder();
        sb.append(digits(uuid.getMostSignificantBits() >> 32, 8));
        sb.append(digits(uuid.getMostSignificantBits() >> 16, 4));
        sb.append(digits(uuid.getMostSignificantBits(), 4));
        sb.append(digits(uuid.getLeastSignificantBits() >> 48, 4));
        sb.append(digits(uuid.getLeastSignificantBits(), 12));
        return sb.toString();
    }
}
